/**
 * Created by alice on 2017/5/03 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        page_size: 12
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();
        Class.count_meeting();
        function init_event() {//TODO 初始化页面事件
            $("#add").click(function () {
                document.getElementById("form").reset();
                $('#summernote').summernote('code', "");
                $("#_editCourtInfo").modal("show");
            });
            //查询的开始时间默认给今天
            $('#startTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });
            $('#endTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });
            $('#_operation_time').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });
            $('#_complete_time').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });
            $('#summernote').summernote({
                height: 300,                 // set editor height
                minHeight: null,             // set minimum height of editor
                maxHeight: null, // set maximum height of editor
                lang: 'zh-CN',
                codemirror: { // codemirror options
                    theme: 'monokai'
                }
                // focus: true                  // set focus to editable area after initializing summernote
            });
            $("#sys_court_status").find("a").attr("href", r.base_path + "system_monitor/sys_court_status?courtUid=" + r.court_id);
            $("#sys_play_info").find("a").attr("href", r.base_path + "system_monitor/sys_play_info?court=Uid" + r.court_uid);
            $("#sys_fault_condition").find("a").attr("href", r.base_path + "views/system_monitor/sys_fault_condition.html?court=" + r.court_id);
            $("#sys_server_status").find("a").attr("href", r.base_path + "system_monitor/sys_server_status?courtUid=" + r.court_uid);
            $("#sys_warning_message").find("a").attr("href", r.base_path + "system_monitor/sys_warning_message?courtUid=" + r.court_uid);
            $("#sys_equipment_management").find("a").attr("href", r.base_path + "views/system_monitor/sys_equipment_management.html?court=" + r.court_id);
            $("#sys_log_info").find("a").attr("href", r.base_path + "system_monitor/sys_log_info?courtUid=" + r.court_uid);

            $('.panel-body').focus(function () {
                var markupStr = $('#summernote').summernote('code');
                if (markupStr == "请输入会议内容") {
                    $('#summernote').summernote('code', "")
                }
            });
            $('.panel-body').blur(function () {
                var markupStr = $('#summernote').summernote('code');
                if (!markupStr) {
                    $('#summernote').summernote('code', "请输入会议内容");
                }
            });
            var table = $('#table');
            table.bootstrapTable({
                columns: [{
                    field: 'Number',
                    title: '序号',
                    width: '5.3%',
                    formatter: function (value, row, index) {
                        return (index + 1) > 9 ? index + 1 : '0' + (index + 1);
                    }
                },
                    {
                        field: 'meetingMaster',
                        title: '与会发起人',
                        width: "20%"
                    },
                    {
                        field: 'meetingTime',
                        sortName: 'meeting_time',
                        title: '发起时间',
                        sortable: true
                    },
                    {
                        field: 'meetingPlace',
                        title: '会议地点'
                    },
                    {
                        field: 'meetingTopic',
                        title: '与会主题'
                    },
                    {
                        field: 'meetingUser',
                        title: '与会人员',
                        width: "20%"
                    },
                    {
                        field: 'edit',
                        title: '操作',
                        sortable: false,
                        class: 'center sm_operation',
                        formatter: function (value, row) {
                            // console.log(row.uid);
                            var html = '<a href="javascript:p.meeting_detail(\'' + row.uid + '\')"><i class="fa fa-file-text fa-lg"></i></a>&nbsp;' +
                                '<a href="javascript:p.meeting_edit(\'' + row.uid + '\')"><i class="fa fa-edit fa-lg"></i></a>&nbsp;' +
                                '<a href="javascript:p.meeting_delete(\'' + row.uid + '\')" ><i class="fa fa-trash-o fa-lg"></i></a>';
                            return html;
                        }
                    }],
                showFooter: false,
                method: 'get',
                sortName: 'createTime',
                sortOrder: 'desc',
                sortStable: true,
                onSort: function (name, order) {
                    Class.select_meeting(1, name, order);
                }
            });
            table.bootstrapTable("hideLoading");
            table.addClass('context-span');
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        count_meeting: function () {
            var meetingMaster = $("#search_court_input").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            $.ajax({
                url: r.base_path + "state/countCourtMeeting",
                type: "get",
                data: {
                    meetingMaster: meetingMaster,
                    startTime: startTime,
                    endTime: endTime
                },
                dataType: "json",
                success: function (result) {
                    // console.log(result);
                    if (result.data == 0) {
                        Class.page(1, 1, null);
                    } else {
                        Class.page(1, Math.ceil(result.data / ready.page_size), Class.select_meeting);
                    }
                    Class.select_meeting(1);
                },
                error: function () {
                    layui.use('layer', function () {
                        layer.msg('查询发生了错误！');
                    });
                    $('#loading').hide();
                }
            });
        },
        select_meeting: function (page_no, name, order) {
            var meetingMaster = $("#search_court_input").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            $.get(r.base_path + "state/selectCourtMeeting", {
                meetingMaster: meetingMaster,
                startTime: startTime,
                endTime: endTime,
                pageSize: ready.page_size,
                pageNo: page_no,
                sort: order,
                order: name
            }, function (result) {
                if (result && result.status == 0) {
                    $('#table').bootstrapTable("load", result.data);
                }
            }, 'json');
            $("#loading").hide();
        },
        meeting_detail: function (uid) {
            $.get(r.base_path + "state/selectCourtMeetingByPrimaryKey", {
                uid: uid
            }, function (result) {
                if (result && result.status == 0) {
                    $('#_meeting_master').html("会议发起人：" + result.data.meetingMaster);
                    $('#_meeting_context').html(result.data.meetingContext);
                    $('#_meeting_time').html("会议时间：" + result.data.meetingTime);
                    $('#_meeting_place').html("会议地点：" + result.data.meetingPlace);
                    $('#_meeting_user').html("与会人员：" + result.data.meetingUser);
                    $('#_meeting_topic').html("会议主题：" + result.data.meetingTopic);
                    $("#_detail").modal("show");
                } else {
                    ready.layer.msg('查找失败！');
                }
            }, 'json').fail(function () {
                ready.layer.msg('查找失败！');
            })
        },
        meeting_edit: function (uid) {
            $.get(r.base_path + "state/selectCourtMeetingByPrimaryKey", {
                uid: uid
            }, function (result) {
                if (result && result.status == 0) {
                    var form = $('#form');
                    form.find("input[name='uid']").val(result.data.uid);
                    form.find("input[name='meetingMaster']").val(result.data.meetingMaster);
                    form.find("input[name='meetingTime']").val(result.data.meetingTime);
                    form.find("input[name='meetingPlace']").val(result.data.meetingPlace);
                    form.find("input[name='meetingUser']").val(result.data.meetingUser);
                    form.find("input[name='meetingTopic']").val(result.data.meetingTopic);
                    $('#summernote').summernote('code', result.data.meetingContext);
                    $("#_editCourtInfo").modal("show");
                } else {
                    ready.layer.msg('查找失败！');
                }
            }, 'json').fail(function () {
                ready.layer.msg('查找失败！');
            })
        },
        meeting_delete: function (uid) {
            ready.layer.confirm('确定删除吗？', {
                btn: ['删除', '取消'] //按钮
            }, function () {
                $.post(r.base_path + "state/deleteCourtMeeting", {
                    uid: uid
                }, function (result) {
                    if (result && result.status == 0) {
                        ready.layer.msg('删除成功！');
                        Class.count_meeting();
                    } else {
                        ready.layer.msg('删除失败！');
                    }
                }, 'json')
            }, function () {
            });
        },
        update_btn: function () {
            var uid = $('#form').find("input[name='uid']").val();
            var meetingMaster = $('#form').find("input[name='meetingMaster']").val();
            var meetingTime = $('#form').find("input[name='meetingTime']").val();
            var meetingPlace = $('#form').find("input[name='meetingPlace']").val();
            var meetingUser = $('#form').find("input[name='meetingUser']").val();
            var meetingTopic = $('#form').find("input[name='meetingTopic']").val();
            var markupStr = $('#summernote').summernote('code');

            if (!meetingMaster || !meetingTopic) {
                ready.layer.msg('请补充完整数据！');
                return;
            }

            if (uid) {
                $.post(r.base_path + "state/updateCourtMeeting", {
                    uid: uid,
                    meetingMaster: meetingMaster,
                    meetingTime: meetingTime,
                    meetingPlace: meetingPlace,
                    meetingUser: meetingUser,
                    meetingTopic: meetingTopic,
                    meetingContext: markupStr
                }, function (result) {
                    if (result && result.status == 0) {
                        ready.layer.msg('更新成功');
                        $("#_editCourtInfo").modal("hide");
                        Class.count_meeting();
                    } else {
                        ready.layer.msg('更新失败！');
                    }
                }, 'json')
            } else {
                $.post(r.base_path + "state/insertCourtMeeting", {
                    meetingMaster: meetingMaster,
                    meetingTime: meetingTime,
                    meetingPlace: meetingPlace,
                    meetingUser: meetingUser,
                    meetingTopic: meetingTopic,
                    meetingContext: markupStr
                }, function (result) {
                    if (result && result.status == 0) {
                        ready.layer.msg('添加成功');
                        $("#_editCourtInfo").modal("hide");
                        Class.count_meeting();

                    } else {
                        $("#_editCourtInfo").modal("hide");
                        ready.layer.msg('添加失败！');
                    }
                }, 'json')
            }
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        count_meeting: Class.count_meeting,
        meeting_detail: Class.meeting_detail,
        meeting_edit: Class.meeting_edit,
        meeting_delete: Class.meeting_delete,
        update_btn: Class.update_btn
    };
}(window);